from Common.handle_request import send_requests
from Common.handle_data import EnvData, replace_case_by_regular, replace_extract_data, clear_EnvData_attrs
from Common.handle_path import uploadfile_dir
from Common.handle_config import conf
from Common.my_logger import logger
import requests
import jsonpath
import os


class CommonTest():

    # 获取项目（第一个）
    def Get_Exam(self):
        logger.info('*********************************获取项目*************************************')
        url = "/ExamAPI/Common/Project/PCListV2"
        headers = {"Content_Type": "application/json", 'token': '#token#'}
        data = {"pageSize": "20", "curPage": "1", "e_using": "1", "edu_levelid": "0", "grd_no": "", "e_name": "",
                "userNo": "#UserNo#", "status": "1"}
        case = {"e_dbname": "$.datas.exams..e_dbname", "e_id": "$.datas.exams..e_id"}
        data = replace_case_by_regular(data)
        headers = replace_case_by_regular(headers)
        resp = send_requests('post', url, data, headers)
        replace_extract_data(case, resp)

    # 查询添加科目（第一个）
    def Gte_Subject(self):
        logger.info('*********************************查询添加科目*************************************')
        url = "/ExamAPI/Exam/Subject/List"
        headers = {"Content_Type": "application/json", 'token': '#token#', "exam_no": "#e_id#"}
        data = {"exam_no": "#e_id#", "e_dbname": "#e_dbname#", "sch_no": ""}
        case = {"recordid": "$..datas[0].recordid", "esub_no": "$..datas[0].esub_no",
                "esub_name": "$..datas[0].esub_name"}
        data = replace_case_by_regular(data)
        headers = replace_case_by_regular(headers)
        resp = send_requests('post', url, data, headers)
        replace_extract_data(case, resp)

    # 用户登陆
    def Login(self):
        logger.info('*********************************登录*************************************')
        UserNo = conf.get('user_1', 'UserNo')
        UserPwd = conf.get('user_1', 'UserPwd')
        url = "/BaseDataApi/UserLogin/Login"
        headers = {'Content_Type': 'application/json'}
        data = {'UserNo': UserNo, 'UserPwd': UserPwd}
        case = {"token": "$..Token", "UnitId": "$..UserInfo.UnitId", "UserName": "$..UserInfo.UserName",
                "userNo": "$..UserInfo.UserNo", "UserNo": "$..UserInfo.UserNo"}
        resp = send_requests('post', url, data, headers)
        replace_extract_data(case, resp)

    # 新建项目(联考；不分科)
    def Save2004(self):
        self.Login()  # 登录
        logger.info('*********************************新建项目*************************************')
        url = "/ExamAPI/Common/Project/Save2004"
        headers = {"Content_Type": "application/json", 'token': '#token#'}
        data = {"is_online_exam": 0, "e_readtype": "识别条码", "e_name": "联考不分科单接口测试", "edu_levelid": 1, "edu_level": "高中",
                "grd_no": "03", "grd_name": "高一", "starttime": "2022-11-04", "mark_closetime": "2022-11-09 23:59:59",
                "is_union": 1, "is_rangescore": 0, "is_pcmark": 1, "is_appmark": 1, "is_qywx_mark": 0,
                "is_tchqueryscore": 1, "is_queryscore": 1, "is_get_error_book": 1, "error_book_amount": 6.9,
                "is_get_answer_sheet": 1, "answer_sheet_amount": 2, "is_get_knowledge_point": 1, "knowledge_amount": 0,
                "u_name": "#UserName#", "IsAppend": 1, "model_name": "", "choose_sub_type_id": "",
                "choose_sub_type_name": "", "choose_sub_type_subType": "", "e_id": "", "sub_type": 0,
                "sch_no": "#UserNo#", "organiser": "", "edu_start": 2022, "e_using": 1, "UserId": "cswang1",
                "u_id": "cswang1", "IsCloud": 0, "tch_no": "0101", "changemodel": 0, "e_examtype": "周考",
                "sch_id": "#UnitId#"}
        data = replace_case_by_regular(data)
        headers = replace_case_by_regular(headers)
        send_requests('post', url, data, headers)
        self.Get_Exam()

    # 删除项目
    def Delete_Exam(self):
        self.Login()  # 登录
        self.Get_Exam()  # 获取项目
        logger.info('*********************************删除项目*************************************')
        url = "/ExamAPI/Common/Project/CompleteExamMission"
        headers = {'Content_Type': 'application/json', 'token': '#token#'}
        data = {"e_id": "#e_id#", "state": "true"}
        data = replace_case_by_regular(data)
        headers = replace_case_by_regular(headers)
        send_requests('post', url, data, headers)

    # 添加科目（语文/理科综合）
    def Add_Subject(self, id=None):
        '''
        :param id: 不传创建语文科目；传1创建理科综合
        '''
        self.Save2004()  # 创建项目
        logger.info('*********************************添加科目*************************************')
        url = "/ExamAPI/Exam/Subject/SaveList1911"
        headers = {'Content_Type': 'application/json', 'token': '#token#'}
        data = {"exam_no": "#e_id#", "e_dbname": "#e_dbname#", "sch_no": "#userNo#", "data": [
            {"esub_no": "", "use_no": "#userNo#", "sub_name": "语文", "sub_type": "普通", "sub_no": "01", "stat_type": "0",
             "online_start_time": "", "online_end_time": "", "esub_name": "语文",
             "SubNameStr": [{"SubNo": "01", "SubName": "语文"}], "stat_type_name": "普通科目", "is_rangescore": 0}]}
        date = {"exam_no": "#e_id#", "e_dbname": "#e_dbname#", "sch_no": "#userNo#", "data": [
            {"esub_no": "", "use_no": "#userNo#", "sub_name": "物理,化学,生物", "sub_type": "普通", "sub_no": "11,12,13",
             "stat_type": "3", "online_start_time": "", "online_end_time": "", "esub_name": "理科综合",
             "SubNameStr": [{"SubName": "物理", "SubNo": "11"}, {"SubName": "化学", "SubNo": "12"},
                            {"SubName": "生物", "SubNo": "13"}], "stat_type_name": "综合科目", "is_rangescore": 0}]}
        data = replace_case_by_regular(data)
        date = replace_case_by_regular(date)
        headers = replace_case_by_regular(headers)
        if id is None:
            send_requests('post', url, data, headers)
        else:
            send_requests('post', url, date, headers)
        self.Gte_Subject()  # 查询添加的科目

    # 选用科目（1.校验）
    def Choose_Subject_verify(self):
        '''
        :param from_exam_no:  复用的项目编号
        :param from_esub_no:  复用的科目编号
        :param from_esub_name:  复用的科目名称
        '''
        self.Add_Subject()  # 添加科目
        logger.info('*********************************选用科目(1.校验)*************************************')
        url = "/ExamAPI/Exam/Subject/CheckSaveOptionSubInfo"
        headers = {"Content_Type": "application/json", 'token': '#token#', "exam_no": "#e_id#"}
        data = {"from_exam_no": "#from_exam_no#", "to_exam_no": "#e_id#", "from_esub_no": "#from_esub_no#",
                "from_esub_name": "#from_esub_name#",
                "to_esub_no": "#esub_no#", "to_esub_name": "#esub_name#", "is_structure": 1, "is_answer": 1,
                "is_knp": 1}
        data = replace_case_by_regular(data)
        headers = replace_case_by_regular(headers)
        send_requests('post', url, data, headers)

    # 添加项目校验
    def Choose_Subject(self):
        self.Choose_Subject_verify()  # 添加科目
        logger.info('*********************************选用科目（2.选用）*************************************')
        url = "/ExamApiV2/api/v1/subject/SubjectChoose"
        headers = {"Content_Type": "application/json", 'token': '#token#', "exam_no": "#e_id#"}
        data = {"examNo": "#e_id#", "esubNo": "#esub_no#", "fromExamNo": "#from_exam_no#",
                "fromEsubNo": "#from_esub_no#",
                "reusePaperSubInfos": [1, 2, 3]}
        data = replace_case_by_regular(data)
        headers = replace_case_by_regular(headers)
        send_requests('post', url, data, headers)

    # 查询模板主观题
    def Get_SubjectiveList(self):
        self.Login()
        logger.info('*********************************查询模板主观题*************************************')
        url = "/ExamApiV2/api/v1/queparameter/GetSubjectiveList"
        headers = {"Content_Type": "application/json", 'token': '#token#', "exam_no": "101246"}
        data = {"exam_no": "101246", "esubNo": "101246001"}
        case = {"SubjectiveList": "$..datas"}
        data = replace_case_by_regular(data)
        headers = replace_case_by_regular(headers)
        resp = send_requests('post', url, data, headers)
        replace_extract_data(case, resp)

    # 批量编辑保存主观题
    def SetSubjectiveScore(self):
        self.Get_SubjectiveList()
        logger.info('*********************************批量编辑保存主观题*************************************')
        url = "/ExamApiV2/api/v1/queparameter/SetSubjectiveScore"
        headers = {"Content_Type": "application/json", 'token': '#token#', "exam_no": "101246"}
        data = {"esubNo": "101246001","setSubJectScoreInfos":getattr(EnvData,'setSubJectScoreInfos')}
        data = replace_case_by_regular(data)
        headers = replace_case_by_regular(headers)
        resp = send_requests('post', url, data, headers)
        return resp

    # 文件上传-导入学校
    def File_School_Upload(self):
        logger.info('*********************************文件上传*************************************')
        url = "/ExamAPI/Exam/school/Import"
        headers = replace_case_by_regular({'Token': '#token#', "exam_no": "#e_id#"})
        data = replace_case_by_regular(
            {"exam_no": "#e_id#", "e_dbname": "#e_dbname#", "use_no": "#userNo#", "IsClearAll": "0"})
        file = {'file': open(os.path.join(uploadfile_dir, 'Schools_upload.xlsx'), 'rb')}
        resp = send_requests('post', url, data, headers, file)

    # 图片上传
    def Img_Upload(shef):
        logger.info('*********************************图片上传*************************************')
        file = {"file": ("1.jpg", open("1.jpg", "rb"), "images/jpeg")}
        pass
    def test(self):
        url = "https://onlinexam.wylkyj.com/api/v1/subject/CommitAnswer"
        headers = {"Content-Type": "application/json","Accept": "text/plain","Token": "Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJFeGFtTm8iOiI2ODcxMSIsIlNjaE5vIjoiV1kwMyIsIkdyYWRlTm8iOiIwMyIsIlN0dU5vIjoiOTUyMjM0MDYyMiIsIm5iZiI6MTY3NTEyOTAyMywiZXhwIjoxNjc1MjE1NDIzLCJpYXQiOjE2NzUxMjkwMjMsImlzcyI6Ik9ubGluZUV4YW1Jc3N1ZXIiLCJhdWQiOiJPbmxpbmVFeGFtQXVkaWVuY2UifQ.yVE5eE-aXu0m3cFen-LqlTVX-KDl5TRWAQhuqtugR88wqCFkfUW77BOzyiem_6qtQrLMS3XV_mLzhPRZpNUt6w"}
        # url = "https://app.wylkyj.com/api/v1/user/Login"
        data = {
  "examNo": "68711",
  "examName": "测试压测在线考试",
  "esubNo": "68711001",
  "stuNo": "9522340622",
  "token": "Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJFeGFtTm8iOiI2ODcxMSIsIlNjaE5vIjoiV1kwMyIsIkdyYWRlTm8iOiIwMyIsIlN0dU5vIjoiOTUyMjM0MDYyMiIsIm5iZiI6MTY3NTEyODYwNywiZXhwIjoxNjc1MjE1MDA3LCJpYXQiOjE2NzUxMjg2MDcsImlzcyI6Ik9ubGluZUV4YW1Jc3N1ZXIiLCJhdWQiOiJPbmxpbmVFeGFtQXVkaWVuY2UifQ.D8gsIg7VsTBZA5EsCghkqJw-65jfqUzoOTafp7Ah7Lh_frOPfiQ4ColD4kzb2gsT0aRMKLGmNQZihRWvBTZ2GA",
  "stuName": "胡雨鑫",
  "schNo": "WY03",
  "isCommitCount": 0,
  "commitInfoDtos":[
      {
        "queName": "单选1",
        "queScore": 2,
        "orderIndex": 1,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06001",
            "queName": "单选1",
            "queScore": 2,
            "indexNo": 0,
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "orderIndex": 1,
            "stuAnswer": ["B"]
          }
        ]
      },
      {
        "queName": "单选2",
        "queScore": 2,
        "orderIndex": 2,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06001",
            "queName": "单选2",
            "queScore": 2,
            "indexNo": 0,
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "orderIndex": 2,
            "stuAnswer": ["A"]
          }
        ]
      },
      {
        "queName": "单选3",
        "queScore": 2,
        "orderIndex": 3,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06001",
            "queName": "单选3",
            "queScore": 2,
            "indexNo": 0,
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "orderIndex": 3,
            "stuAnswer": ["A"]
          }
        ]
      },
      {
        "queName": "单选4",
        "queScore": 2,
        "orderIndex": 4,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06001",
            "queName": "单选4",
            "queScore": 2,
            "indexNo": 0,
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "orderIndex": 4,
            "stuAnswer": ["A"]
          }
        ]
      },
      {
        "queName": "单选5",
        "queScore": 2,
        "orderIndex": 5,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06001",
            "queName": "单选5",
            "queScore": 2,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "queInfo": "null",
            "orderIndex": 5,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "单选6",
        "queScore": 2,
        "queInfo": "null",
        "orderIndex": 6,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06002",
            "queName": "单选6",
            "queScore": 2,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "queInfo": "null",
            "orderIndex": 6,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "单选7",
        "queScore": 2,
        "queInfo": "null",
        "orderIndex": 7,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06002",
            "queName": "单选7",
            "queScore": 2,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "queInfo": "null",
            "orderIndex": 7,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "单选8",
        "queScore": 2,
        "queInfo": "null",
        "orderIndex": 8,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06002",
            "queName": "单选8",
            "queScore": 2,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "queInfo": "null",
            "orderIndex": 8,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "单选9",
        "queScore": 2,
        "queInfo": "null",
        "orderIndex": 9,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06002",
            "queName": "单选9",
            "queScore": 2,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "queInfo": "null",
            "orderIndex": 9,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "单选10",
        "queScore": 2,
        "queInfo": "null",
        "orderIndex": 10,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 4,
            "optType": "单选",
            "symbol": [
              "A",
              "B",
              "C",
              "D"
            ],
            "queNo": "06002",
            "queName": "单选10",
            "queScore": 2,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 1,
            "queType": "选择题",
            "queInfo": "null",
            "orderIndex": 10,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "短",
        "queScore": 10,
        "queInfo": "null",
        "orderIndex": 11,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 1,
            "optType": "null",
            "symbol": [],
            "queNo": "07001",
            "queName": "短",
            "queScore": 10,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 0,
            "queType": "解答题",
            "queInfo": "null",
            "orderIndex": 11,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "中",
        "queScore": 10,
        "queInfo": "null",
        "orderIndex": 12,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 1,
            "optType": "null",
            "symbol": [],
            "queNo": "07002",
            "queName": "中",
            "queScore": 10,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 0,
            "queType": "解答题",
            "queInfo": "null",
            "orderIndex": 12,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "长",
        "queScore": 10,
        "queInfo": "null",
        "orderIndex": 13,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 1,
            "optType": "null",
            "symbol": [],
            "queNo": "07003",
            "queName": "长",
            "queScore": 10,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 0,
            "queType": "解答题",
            "queInfo": "null",
            "orderIndex": 13,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "中短",
        "queScore": 10,
        "queInfo": "null",
        "orderIndex": 14,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 1,
            "optType": "null",
            "symbol": [],
            "queNo": "07004",
            "queName": "中短",
            "queScore": 10,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 0,
            "queType": "解答题",
            "queInfo": "null",
            "orderIndex": 14,
            "stuAnswer": []
          }
        ]
      },
      {
        "queName": "作文",
        "queScore": 10,
        "queInfo": "null",
        "orderIndex": 15,
        "isChoicQue": 0,
        "queList": [
          {
            "optCount": 1,
            "optType": "null",
            "symbol": [],
            "queNo": "07005",
            "queName": "作文",
            "queScore": 10,
            "indexNo": 0,
            "choiceName": "null",
            "isChecked": 0,
            "isChoice": 0,
            "isCchoice": 0,
            "isObject": 0,
            "queType": "解答题",
            "queInfo": "null",
            "orderIndex": 15,
            "stuAnswer": []
          }
        ]
      }
    ]
}

        datas = requests.post(url,headers=headers,json=data)
        print(datas.text)


if __name__ == '__main__':
    clear_EnvData_attrs()
    # print(dict(EnvData.__dict__))
    com = CommonTest()
    com.test()